package com.yx.cloud.utils;

import android.graphics.Point;
import android.util.Log;

import static java.lang.Math.abs;


/**
 * Created by Administrator on 2019/8/14.
 */

/**
 * 首先利用绝对值把 矩形中心 - 圆心 转移到第一象限
 * circleCenter圆心的坐标，boxCenter矩形中心点的坐标，circleR圆的半径长，boxHeight长方形的半长()
 * <p>
 * 这种方法需要矩形的边需要旋转到与x y轴平行  并且 把坐标原点移动到矩形中心  然后再把圆心绝对值到第一象限 再判断
 */
public class IntersectUtils {
    private static final String TAG = "DragScaleViewRotate";

    public static boolean BoxCircleIntersect(Point boxCenter, Point circleCenter, Point boxHeight, float circleR) {
        Point v = new Point(abs(circleCenter.x - boxCenter.x), abs(circleCenter.y - boxCenter.y));
        Log.d("circle", "point x:" + v.x + "==== point y:" + v.y);
        Point u = new Point(v.x - boxHeight.x > 0 ? v.x - boxHeight.x : 0, v.y - boxHeight.y > 0 ? v.y - boxHeight.y : 0);
        return u.x * u.x + u.y * u.y < circleR * circleR;
    }

    /**
     * 判断禁区是否在在充电桩保护范围内
     */
    public static boolean isResctrictCrossChargePile(int cx, int cy, int r, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
        //1 判断圆是否在矩形内
        //2 判断矩形的四个点是否在圆内
        //3 判断圆心与矩形四条边的距离是否小于半径
        if (isCircleInRectangle(cx, cy, r, x1, y1, x2, y2, x3, y3, x4, y4) ||
                isRectangleInCircle(cx, cy, r, x1, y1, x2, y2, x3, y3, x4, y4) ||
                isRectangleCrossCircle(cx, cy, r, x1, y1, x2, y2, x3, y3, x4, y4)) {
            return true;
        }
        return false;
    }

    private static boolean isCircleInRectangle(int cx, int cy, int r, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
        //判断圆是否在矩形内：判断圆心与四点构成的四个三角形面积之和是否大于矩形面积
        double sum = 0;
        sum += triangleArea(cx, cy, x1, y1, x2, y2);
        sum += triangleArea(cx, cy, x2, y2, x3, y3);
        sum += triangleArea(cx, cy, x3, y3, x4, y4);
        sum += triangleArea(cx, cy, x4, y4, x1, y1);
        double polyArea = 2 * triangleArea(x1, y1, x2, y2, x3, y3);
        Log.i(TAG, "sum triangleArea: : " + sum + "  polyArea: " + polyArea);
        if (sum > polyArea) {
            Log.i(TAG, "isResctrictCrossChargePile isCircleInRectangle: false");
            return false;
        }
        Log.i(TAG, "isResctrictCrossChargePile isCircleInRectangle: true");
        return true;
    }


    private static boolean isRectangleInCircle(int cx, int cy, int r, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
        // 判断矩形的四个点是否在圆内：判断圆心到4点距离是否小于半径
        if (distance(cx, cy, x1, y1) <= r
                || distance(cx, cy, x2, y2) <= r
                || distance(cx, cy, x3, y3) <= r
                || distance(cx, cy, x4, y4) <= r) {
            Log.i(TAG, "isResctrictCrossChargePile isRectangleInCircle: true");
            return true;
        }
        Log.i(TAG, "isResctrictCrossChargePile isRectangleInCircle: false");
        return false;
    }

    private static boolean isRectangleCrossCircle(int cx, int cy, int r, int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
        //判断圆心与矩形四条边的距离是否小于半径
        Log.i(TAG, "r : " + r);
        if (pointToLine(cx, cy, x1, y1, x2, y2) <= r
                || pointToLine(cx, cy, x2, y2, x3, y3) <= r
                || pointToLine(cx, cy, x3, y3, x4, y4) <= r
                || pointToLine(cx, cy, x1, y1, x4, y4) <= r) {
            Log.i(TAG, "isResctrictCrossChargePile isRectangleCrossCircle: true");
            return true;
        }
        Log.i(TAG, "isResctrictCrossChargePile isRectangleCrossCircle: false");
        return false;
    }

    /**
     * 计算点到线段距离
     *
     * @param cx
     * @param cy
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     * @return
     */
    private static double pointToLine(int cx, int cy, int x1, int y1, int x2, int y2) {
        double space = 0;
        double a, b, c;
        a = distance(x1, y1, x2, y2);// 线段的长度
        b = distance(x1, y1, cx, cy);// (x1,y1)到点的距离
        c = distance(x2, y2, cx, cy);// (x2,y2)到点的距离
        Log.i(TAG, "a: " + a + " b : " + b + " c : " + c);
        if (c <= 0.000001 || b <= 0.000001) {
            space = 0;
            return space;
        }
        if (a <= 0.000001) {
            space = b;
            return space;
        }
        if (c * c >= a * a + b * b) {
            space = b;
            return space;
        }
        if (b * b >= a * a + c * c) {
            space = c;
            return space;
        }
        // 海伦公式求面积
        double p = (a + b + c) / 2;// 半周长
        double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));
        space = 2 * s / a;// 返回点到线的距离（利用三角形面积公式求高）
        return space;
    }

    public static double distance(int x1, int y1, int x2, int y2) {
        return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    }

    //计算三角形的面积
    private static double triangleArea(int x1, int y1, int x2, int y2, int x3, int y3) {
        return 0.5 * Math.abs((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1));
    }
}

